const { PHASE_DEVELOPMENT_SERVER } = require('next/constants');
const packageInfo = require('./package.json');
const SERVER_MODE_API_PROXY = 'api-proxy'; // [To-Do] move to another file.
const SERVER_MODE_LOCALHOST = 'localhost';

const webpack = require('webpack');

/**
 * Caveats to get local IP address of running host.
 * Reference - https://gist.github.com/sviatco/9054346
 */
const getLocalExternalIP = () => {
  const ifaces = require('os').networkInterfaces();
  let ip;
  Object.keys(ifaces).forEach(dev => {
    ifaces[dev].filter(details => {
      if (details.family === 'IPv4' && details.internal === false) {
        ip = details.address;
      }
    });
  });
  console.log('local IP: ' + ip);
  return ip;
};

/**
 * For custom advanced behavior of Next.js,
 * you can create a next.config.js in the root of your project directory (next to package.json).
 *
 * next.config.js is a regular Node.js module, not a JSON file.
 * It gets used by the Next.js server and build phases, and it's not included in the browser build.
 *
 * Please visit for details
 * - https://nextjs.org/docs/api-reference/next.config.js/introduction
 */

module.exports = phase => {
  // when started in development mode `next dev` or `yarn dev` or by custom server.
  let mode = process.env.SERVER_MODE || SERVER_MODE_LOCALHOST;
  console.info('Server is running as ' + mode);

  return {
    /* config options for all phases except development here */
    env: {
      APP_NAME: packageInfo.name,
      APP_VERSION: packageInfo.version,
    },
    /* Since version 10.2, Next.js has built-in web font optimization. */
    optimizeFonts: false,
    webpack: (config, { dev, isServer }) => {
      if (!dev) {
        config.devtool = isServer ? false : 'hidden-source-map';
      }
      // markdown support
      config.module.rules.push({
        test: /\.md$/,
        use: 'raw-loader',
      });
      return {
        ...config,
        mode: config.mode,
        plugins: [
          ...config.plugins,
          new webpack.ContextReplacementPlugin(/moment[/\\]locale$/, /^\.\/ko$/),
        ],
      };
    },
    typescript: {
      // !! WARN !!
      // Dangerously allow production builds to successfully complete even if
      // your project has type errors.
      // !! WARN !!
      ignoreBuildErrors: true,
    },
  };
};

// module.exports = {
//   typescript: {
//     // !! WARN !!
//     // Dangerously allow production builds to successfully complete even if
//     // your project has type errors.
//     // !! WARN !!
//     ignoreBuildErrors: true,
//   },
// };